package cn.tedu.ivoscm.user.controller;

import cn.tedu.ivoscm.base.response.JsonResult;
import cn.tedu.ivoscm.user.pojo.dto.UserLoginParam;
import cn.tedu.ivoscm.user.pojo.dto.UserQuery;
import cn.tedu.ivoscm.user.pojo.dto.UserSaveParam;
import cn.tedu.ivoscm.user.pojo.vo.UserVO;
import cn.tedu.ivoscm.user.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
@Api(tags = "01.用户管理")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("login")
    @ApiOperation(value = "用户登录")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@RequestBody @Validated UserLoginParam userLoginParam) {
        log.debug("用户登录：{}", userLoginParam);
        //Alt+Enter生成方法  Ctrl去接口  Ctrl+Alt去接口实现类
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @PostMapping("save")
    @ApiOperation(value = "保存用户信息")
    @ApiOperationSupport(order = 20)
    public JsonResult save(@Validated UserSaveParam usp) {
        log.debug("保存用户：{}", usp);
        userService.saveUser(usp);
        return JsonResult.ok();
    }

    @GetMapping("select")
    @ApiOperation(value = "查询用户列表")
    @ApiOperationSupport(order = 30)
    public JsonResult selectUser(UserQuery userQuery) {
        log.debug("查询用户列表，参数如下：{}", userQuery);
        List<UserVO> usv = userService.list(userQuery);
        return JsonResult.ok(usv);
    }

    @GetMapping("select/audit/{parentId}")
    @ApiOperation(value = "查询审批人列表")
    @ApiOperationSupport(order = 31)
    public JsonResult selectUser(@PathVariable Long parentId) {
        log.debug("查询审批人列表，参数如下：{}", parentId);
        List<UserVO> usv = userService.listAudit(parentId);
        return JsonResult.ok(usv);
    }

    @PostMapping("update/password/{id}")
    @ApiOperation(value = "修改密码")
    @ApiOperationSupport(order = 40)
    public JsonResult updatePassword(@PathVariable Long id) {
        log.debug("修改密码，参数如下：{}", id);
        userService.updatePassword(id);
        return JsonResult.ok();
    }

    @PostMapping("update/status/{id}/{status}")
    @ApiOperation(value = "修改账号状态")
    @ApiOperationSupport(order = 50)
    public JsonResult updatePassword(@PathVariable Long id, @PathVariable String status) {
        log.debug("修改账号状态参数：id: {}, status: {}", id, status);
        userService.updateStatus(id, status);
        return JsonResult.ok();
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除用户")
    @ApiOperationSupport(order = 60)
    public JsonResult deleteUser(@PathVariable Long id) {
        log.debug("修改账号状态参数：id: {}", id);
        userService.deleteById(id);
        return JsonResult.ok();
    }
}






